package cn.id68.www.http;

import java.io.IOException;

import cn.id68.www.http.builder.GetBuilder;
import cn.id68.www.http.callback.Callback;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/3/20.
 */
public class OkHttpUtils {

    public static final long DEFAULT_MILLISECONDS = 10_000L;

    private volatile static OkHttpUtils mInstance;
    private OkHttpClient mOkHttpClient;
    private Platform mPlatform;

    public OkHttpUtils(OkHttpClient okHttpClient){
        if (okHttpClient == null){
            mOkHttpClient = new OkHttpClient();
        }else {
            mOkHttpClient = okHttpClient;
        }
        mPlatform = Platform.get();
    }

    public static OkHttpUtils getmInstance(){
        return initClient(null);
    }

    private static OkHttpUtils initClient(OkHttpClient okHttpClient) {
        if (mInstance==null){
            synchronized (OkHttpUtils.class){
                if (mInstance == null){
                    mInstance = new OkHttpUtils(okHttpClient);
                }
            }
        }

        return mInstance;
    }


    public OkHttpClient getmOkHttpClient() {
        return mOkHttpClient;
    }

    public void execute(final RequestCall requestCall, Callback callback){
        if (callback == null){
            callback = Callback.CALLBACK_DEFAULT;
            final Callback finalCallBack = callback;
            final int id = requestCall.getOkHttpRequest().getId();


            requestCall.getCall().enqueue(new okhttp3.Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {

                }
            });

        }
    }

    public static GetBuilder getBuilder(){
        return new GetBuilder();
    }

    public static class METHOD{
        public static final String HEAD = "HEAD";
        public static final String DELETE = "DELETE";
        public static final String PUT = "PUT";
        public static final String PATCH = "PATCH";
    }
}
